<?php


namespace App\HttpController\Admin;



use App\Consts\RedisKey;
use App\HttpController\Admin\AdminBaseController;
use App\Middleware\AuthMiddleware;
use App\Model\Admin;
use App\Model\SystemConfig;
use EasySwoole\EasySwoole\Config;
use App\Utility\Response;
use EasySwoole\RedisPool\Redis;
use App\Model\AdminWhite;
use App\Functions\PHPGangstaGoogleAuthenticator;


class Login extends AdminBaseController
{

    public function login(){

        $username = $this->request()->getRequestParam('username');
        $password = $this->request()->getRequestParam('password');
        $gv_code = $this->request()->getRequestParam('gv_code');
        $ip = $this->request()->getRequestParam('ip');
        $city = $this->request()->getRequestParam('city');

        $admin = Admin::create()->where(['username'=> $username])->get();
        if(!$admin){
            return $this->returnData(1002);
        }

        if($admin['password'] != md5($password)){
            return $this->returnData(1048);
        }
        
        //禁止登录
        if($admin['state'] != 1){
            return $this->returnData(1040);
        }
        
        $white = AdminWhite::create()->field('ip')->where(['admin_id'=>$admin->id])->column();
        
        if(!in_array($ip,$white) && !in_array('*',$white)){
            return $this->returnData(1100);
        }
        
        $gv_status = 0;
        if($admin->gv_code != ''){
            $gv_status = 1;
            
            if($gv_code){
                $ga = new PHPGangstaGoogleAuthenticator();
                
                $checkResult = $ga->verifyCode($admin->gv_code, $gv_code, 2);
                
                if (!$checkResult) {
                    return $this->returnData(1101);
                }
            }else{
                return $this->returnData(1102);
            }
        }
        
        if($ip && $city){
            $admin->ip = $ip;
            $admin->city = $city;
            
            if(!$admin->update()){
                return $this->returnData(500);
            }
        }
        

        
        $jwtObject = \EasySwoole\Jwt\Jwt::getInstance()
            ->setSecretKey(\EasySwoole\EasySwoole\Config::getInstance()->getConf('salt'))
            ->publish();
        $jwtConf = Config::getInstance()->getConf('JWT');
        $jwtObject->setAlg($jwtConf['Alg']); // 加密方式
        $jwtObject->setAud($admin->id); // 用户
        $jwtObject->setExp(time()+ \App\Consts\RedisKey::LOGIN_SESSION_OUTTIME); // 过期时间
        $jwtObject->setIat(time()); // 发布时间
        $jwtObject->setIss($jwtConf['Iss']); // 发行人
        $jwtObject->setJti(md5(time())); // jwt id 用于标识该jwt
        $jwtObject->setNbf(time()+60*5); // 在此之前不可用
        $jwtObject->setSub($jwtConf['Sub']['front']); // 主题
        $jwtObject->setData([
            'manager_info' => $admin
        ]);
        $token = $jwtObject->__toString();
        Redis::defer('redis')->set(RedisKey::user_authority_list($admin->role_id), json_encode(AuthMiddleware::getInstance()->getAuthority($admin->role_id)));
        $base_config['host'] =        SystemConfig::getConf('host');
        $base_config['imgs'] =        Config::getInstance()->getConf('system_upload.path');
        $base_config['qrcode_path'] = Config::getInstance()->getConf('officialAccount_qrcode.path');

        $admin = $admin->toArray();
        unset($admin['password'],$admin['gv_code']);
        $admin['gv_status'] = $gv_status;

        return  $this->returnData(200,['token'=> $token ,'user_info'=> $admin,'base_config' => $base_config]);

    }

    public function test($request, $response){





//        var_dump('res',$redis->set('test',1));
//        var_dump('ping',$redis->ping('111'));


    }
    
}